package cn.fql.system.controller;


import cn.fql.basic.util.AjaxResult;
import cn.fql.basic.util.PageList;
import cn.fql.system.domain.Detail;
import cn.fql.system.query.DetailQuery;
import cn.fql.system.service.IDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DetailController {

    @Autowired
    private IDetailService detailService;
    /**
     Get http://localhost/detail/1
     */
    @GetMapping("/{id}")
    //@PathVariable("id") Long id - 从路径上去获取变量 id 赋值给 Long id
    public Detail  findById(@PathVariable("id") Long id){
        return detailService.findById(id);
    }

    /**接口
     查询所有:
     //Get http://localhost/detail
     */
    @GetMapping
    public List<Detail> findAll(){
        return detailService.findAll();
    }

    /**
     * 接口：分页查询
     * @param detailQuery
     * @return
     */
    //post http://localhost/detail 对象
    //返回值要用PageList中的参数总数量 不然无法计算分页数量
    @PostMapping
    public PageList<Detail> queryPage(@RequestBody DetailQuery detailQuery){
        return detailService.queryPage(detailQuery);
    }


    /**
     * 删除接口
     * @param id
     * @return
     */
    //delete http://localhost/detail/1
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            detailService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加和修改接口
     * @param detail
     * @return
     */
    //put http://localhost/detail {对象}
    //RequestBody  请求体:赋值数据 因为不是get请求 无法从地址栏获取数据
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Detail detail){
        try {
            if(detail.getId() == null){
                detailService.save(detail);
            }else{
                detailService.update(detail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 批量删除接口
     * @param
     * @return
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            detailService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }


}
